package com.mg.plugin.agrometeorology.common;

import com.mg.plugin.agrometeorology.entity.StationData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DataFormatter {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static void formatStationData(List<String> inputRealityList, List<String> inputForecastList, Map<String, String> headerIndex, StationDataType stationDataType, Map<String, List<StationData>> stationDataList) throws ParseException {
        if (null != inputRealityList && inputRealityList.size() > 2) {
            String realHeader = inputRealityList.get(0);
            executeData(inputRealityList, stationDataList, realHeader, stationDataType, headerIndex);
        }
        if (null != inputForecastList && inputForecastList.size() > 2) {
            String realHeader = inputForecastList.get(0);
            executeData(inputForecastList, stationDataList, realHeader, stationDataType, headerIndex);
        }
    }

    private static void executeData(List<String> inputRealityList, Map<String, List<StationData>> stationDataList, String realHeader, StationDataType stationDataType, Map<String, String> headerIndex) throws ParseException {
        if (StationDataType.APHIS_GLYCINES == stationDataType) {
            assistAphisGlycinesStationData(inputRealityList, stationDataList, realHeader, stationDataType, headerIndex);
        } else if (StationDataType.OSTRINIA == stationDataType) {
            assistOstriniaStationData(inputRealityList, stationDataList, realHeader, stationDataType, headerIndex);

        }
    }

    /**
     * String数据转换为对象数据
     *
     * @param inputRealityList
     */
    private static void assistOstriniaStationData(List<String> inputRealityList, Map<String, List<StationData>> stationDataList, String realHeader, StationDataType stationDataType, Map<String, String> headerIndex) {

        int realStationIndex = findDataIndex(realHeader, headerIndex.get("StationId"));
        int realLatIndex = findDataIndex(realHeader, headerIndex.get("Lat"));
        int realLonIndex = findDataIndex(realHeader, headerIndex.get("Lon"));
        int realDateIndex = findDataIndex(realHeader, headerIndex.get("Date"));
        int realRainIndex = findDataIndex(realHeader, headerIndex.get("Rain"));
        int realTempIndex = findDataIndex(realHeader, headerIndex.get("Temp"));
        int realHumidityIndex = findDataIndex(realHeader, headerIndex.get("Humidity"));
        int realMaxTempIndex = findDataIndex(realHeader, headerIndex.get("MaxTemp"));
        for (int i = 2; i < inputRealityList.size(); i++) {
            String stationId = inputRealityList.get(i).split(",")[realStationIndex];
            StationData stationData = new StationData(inputRealityList.get(i), realStationIndex, realDateIndex, realRainIndex, realTempIndex, realHumidityIndex, realMaxTempIndex, StationDataType.OSTRINIA);
            stationData.setLat(Float.parseFloat(inputRealityList.get(i).split(",")[realLatIndex]));
            stationData.setLon(Float.parseFloat(inputRealityList.get(i).split(",")[realLonIndex]));
            if (stationDataList.keySet().contains(stationId)) {
                stationDataList.get(stationId).add(stationData);
            } else {
                List<StationData> dataList = new ArrayList<StationData>();
                dataList.add(stationData);
                stationDataList.put(stationId, dataList);
            }
        }
    }

    private static void assistAphisGlycinesStationData(List<String> inputRealityList, Map<String, List<StationData>> stationDataList, String realHeader, StationDataType stationDataType, Map<String, String> headerIndex) {
        int realStationIndex = findDataIndex(realHeader, headerIndex.get("StationId"));
        int realDateIndex = findDataIndex(realHeader, headerIndex.get("Date"));
        int realRainIndex = findDataIndex(realHeader, headerIndex.get("Rain"));
        int realOldRainIndex = findDataIndex(realHeader, headerIndex.get("OldRain"));
        for (int i = 2; i < inputRealityList.size(); i++) {
            String stationId = inputRealityList.get(i).split(",")[realStationIndex];
            StationData stationData = new StationData(inputRealityList.get(i), realStationIndex, realDateIndex, realRainIndex, realOldRainIndex, stationDataType);

            if (stationDataList.keySet().contains(stationId)) {
                stationDataList.get(stationId).add(stationData);
            } else {
                List<StationData> dataList = new ArrayList<StationData>();
                dataList.add(stationData);
                stationDataList.put(stationId, dataList);
            }
        }
    }

    private static int findDataIndex(String realHeader, String strRain) {

        String[] data = realHeader.split(",");
        for (int i = 0; i < data.length; i++) {
            if (data[i].equalsIgnoreCase(strRain)) {
                return i;
            }
        }
        return -1;
    }

    enum DateType {
        WINTER,
        RECOVER,
        CHEMICAL1,
        CHEMICAL2,
    }
}
